﻿
(function (win, undefined) {
    //游戏实体基础类类
    F5Frame.register("F5Frame", function (cg) {
        this.ObjBase = function () {
            this._id = 0; //相对于对象集合中的主键
            this._x = 0; //x坐标
            this._y = 0; //y坐标
            this.keypoint = [0, 0]; //key点(0,0为图片中心)
            this.imgs = []; //图片列表
            this._image; //当前显示图片
            this._show = true; //显示标识
            this.zOrder = 10; //显示深度(同级)
            this.zLayer = 1; //显示深度(级别)
            this._isform = false;
            this._align = "conter"; //对齐方式:居中  字符存在"left"会左对齐,存在"top"会上对齐
            this._pick = false; //当前拾取状态
            this._onpick = false; //是否可拾取
            this._drag = false; //是否可拖动

            /** @父子对象
            */
            this._childObjs = new Array();
            this._fatherNode = null;

            this.getID = function () {
                return this._id;
            }
            this.setID = function (v) {
                this._id = v;
            }
            this.getX = function () {
                //返回的位置还需要加上key点的偏移
                rx = 0;
                if (this._align.indexOf("left") > 0) {
                    rx = this._x + this.keypoint[0];
                }
                else if (this._align.indexOf("right") > 0) {
                    rx = this._x - (this.getWidth() + this.keypoint[0]);
                }
                else {
                    rx = this._x - (this.getWidth() / 2 + this.keypoint[0]);
                }
                return rx;
            }
            this.setX = function (v) {
                this._x = v;
            }
            this.getY = function () {
                ry = 0;
                if (this._align.indexOf("top") > 0) {
                    ry = this._y + this.keypoint[0];
                }
                else {
                    ry = this._y - this.getHeight() / 2 + this.keypoint[1];
                }
                return ry;
            }
            this.setY = function (v) {
                this._y = v;
            }
            this.getConterY = function () {
                return this._y;
            }
            this.getConterX = function () {
                return this._x;
            }
            this.getImage = function () {
                return this._image;
            }
            this.setImage = function (v) {
                this._image = F5Frame.loader.loadedImgs[v];
            }
            this.getWidth = function () {
                return this._image.width;
            }
            this.getHeight = function () {
                return this._image.height;
            }
            this.show = function () {
                this._show = true;
                //显示所有子节点
                for (x in this._childObjs) {
                    this._childObjs[x].show();
                }
            }
            this.hide = function () {
                this._show = false;
                //隐藏所有子节点
                for (x in this._childObjs) {
                    this._childObjs[x].hide();
                }
            }
            this.isShow = function () {
                return this._show;
            }
            //对齐方式
            this.setAlign = function (v) {
                this._align = "v" + v;
            }
            //坐标偏移
            this.moveOff = function (offx, offy) {
                this.setX(this._x - offx);
                this.setY(this._y - offy);
                //子对象一直偏移
                for (x in this._childObjs) {
                    obj = this._childObjs[x];
                    obj.moveOff(offx, offy);
                }
            }

            //释放对象,把对象从管理器中移除
            this.destroy = function () {
                F5Frame.removeGameObject(this);
                //把子节点一起移除
                for (x in this._childObjs) {
                    this._childObjs[x].destroy();
                }
                //从父节点中脱离
                if (this._fatherNode != null) {
                    this._fatherNode.destroyChild(this);

                    this._fatherNode = null;
                }
            }

            //帧更新
            this.frame = function (/**Number*/dt, /**CanvasRenderingContext2D*/context) {
            }

            //绘制
            this.draw = function (/**Number*/dt, /**CanvasRenderingContext2D*/context) {
                if (this._show) {
                    context.drawImage(this.getImage(), this.getX(), this.getY());
                    this.draw_pick(context);
                }
            }
            //删除资源
            this.delobj = function () {
                F5Frame.removeGameObject(this);
            }

            //选中判定
            this.jugePick = function (px, py) {
                if (this.isShow()) {
                    //判断鼠标是否在其范围内
                    x1 = this.getX();
                    x2 = this.getX() + this.getWidth();
                    y1 = this.getY();
                    y2 = this.getY() + this.getHeight();
                    if (px >= x1 && px <= x2 && py >= y1 && py <= y2) {
                        return true;
                    }
                }
                return false;
            }

            //设置是否可拖动
            this.drag = function (value) {
                var v = value || null;
                if (v == null) {
                    return this._drag;
                }
                else {
                    this._drag = value;
                }
                return false;
            }

            //设置是否可拾取
            this.pick = function (value) {
                value = value || true;
                this._onpick = value;
            }
            this.isOnPick = function () {
                return this._onpick;
            }
            //对象选中
            this.draw_pick = function (context) {
                if (this.isOnPick() && this._pick) {
                    //设置颜色
                    tempcolor = context.strokeStyle;
                    context.strokeStyle = "blue";
                    //生成一个矩形外边
                    context.strokeRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
                    context.strokeStyle = tempcolor;
                }
            }
            //设置拾取状态
            this.set_pick = function (ispick) {
                if (this._pick != ispick) {
                    this._pick = ispick;
                    //增加离开后的事件
                    if (!this._pick)
                        this.pick_out();
                    else
                        this.pick_in();
                }
            }

            //鼠标进入调用事件
            this.pick_in = function () {
                if (this.isOnPick()) {
                    //文字可选择,显示手形
                    document.body.style.cursor = 'pointer';
                }
                else if (this.drag()) {
                    //可拖动显示移动
                    document.body.style.cursor = 'move';
                }
            }

            //鼠标离开后调用事件
            this.pick_out = function () {
                document.body.style.cursor = 'default';
            }

            /*事件*/
            this._clickfunc = null;
            this._clickages = { arg1: null, arg2: null, arg3: null, arg4: null };
            this._clickfunc_right = null;
            this._clickages_right = { arg1: null, arg2: null, arg3: null, arg4: null };
            //点击事件执行
            this._click = function (mx, my, which) {
                var mwhick = which || 1;

                //判断是否选中
                if (this._pick) {
                    if (this._clickfunc || this._clickfunc_right) {
                        //4个参数足够了吧
                        arg1 = null;
                        arg2 = null;
                        arg3 = null;
                        arg4 = null;
                        if (mwhick == 1) {
                            if (this._clickages.hasOwnProperty("arg1")) {
                                arg1 = this._clickages.arg1;
                            }
                            if (this._clickages.hasOwnProperty("arg2")) {
                                arg2 = this._clickages.arg2;
                            }
                            if (this._clickages.hasOwnProperty("arg3")) {
                                arg3 = this._clickages.arg3;
                            }
                            if (this._clickages.hasOwnProperty("arg4")) {
                                arg4 = this._clickages.arg4;
                            }
                            this._clickfunc(arg1, arg2, arg3, arg4, mx, my);
                        }
                        else if (mwhick == 3) {
                            if (this._clickages_right.hasOwnProperty("arg1")) {
                                arg1 = this._clickages_right.arg1;
                            }
                            if (this._clickages_right.hasOwnProperty("arg2")) {
                                arg2 = this._clickages_right.arg2;
                            }
                            if (this._clickages_right.hasOwnProperty("arg3")) {
                                arg3 = this._clickages_right.arg3;
                            }
                            if (this._clickages_right.hasOwnProperty("arg4")) {
                                arg4 = this._clickages_right.arg4;
                            }
                            this._clickfunc_right(arg1, arg2, arg3, arg4, mx, my);
                        }
                    }
                }
            }
            //鼠标点击
            this.click = function (func, args) {
                func = func || false;
                if (func) {
                    this._clickfunc = func;
                    if (args)
                        this._clickages = args;
                    //设置为可拾取
                    this.pick();
                }
                else {
                    if (this._clickfunc) {
                        this._clickfunc();
                    }
                }
            }
            //鼠标点击(右键)
            this.click_right = function (func, args) {
                func = func || false;
                if (func) {
                    this._clickfunc_right = func;
                    if (args)
                        this._clickages_right = args;
                    //设置为可拾取
                    this.pick();
                }
                else {
                    if (this._clickfunc_right) {
                        this._clickfunc_right();
                    }
                }
            }
            //鼠标按下
            this.on_mousedown = function () { }
            //鼠标起来
            this.on_mouseup = function () { }

            //添加子对象,(子对象的坐标是相对于父对象的,父对象移动,子对象会一同移动)
            this.appendChild = function (obj) {
                //设置其坐标为父节点的坐标
                obj.moveOff(-this.getX(), -this.getY());
                obj.setzOrder(obj.zOrder - this.zOrder + 1 + this.zOrder);
                obj.setLayer(this.zLayer);
                obj._fatherNode = this;
                obj._show = this._show;

                F5Frame.sortGameObject();

                this._childObjs.push(obj);
            }

            this.destroyChild = function (obj) {
                for (x in this._childObjs) {
                    if (this._childObjs[x]._id == obj._id) {
                        delete this._childObjs[x];
                        break;
                    }
                }
            }

            this.setLayer = function (layer) {
                for (x in this._childObjs) {
                    this._childObjs[x].setLayer(this._childObjs[x].zLayer - this.zLayer + layer);
                }
                this.zLayer = layer;
            }

            this.setzOrder = function (zOrder) {
                for (x in this._childObjs) {
                    this._childObjs[x].setzOrder(this._childObjs[x].zOrder - this.zOrder + zOrder);
                }
                this.zOrder = zOrder;
            }
        }
    });

})(window, undefined);